clear all
set more off

global mun "H:/Stata/Data/municipality"
global admin "H:/Stata/Data/"

** Bringing in municipality data

import delim "$mun/municipality_v2.csv", clear
drop v1
sort formateur year
bysort formateur : egen howmany = count(year)
tab howmany 
drop howmany // everything appears 7 times

encode formateur, gen (munid)
save "$mun/municipality_v2", replace
keep formateur year
save "$mun/munshort_v2", replace

* Getting Municipality to Province Match

use "K:\Utilities\Code_Listings\Gebieden in Nederland\geconverteerde bestanden/GIN2017V1.dta", clear
gen formateur = "GM" + gemeente

destring provincie, replace
gen west_provincie = 1 if ///
provincie  == 26 | ///
provincie  == 27 | ///
provincie  == 28
replace west_provincie = 0 if ///
provincie  == 20 | ///
provincie  == 21 | ///
provincie  == 22 | ///
provincie  == 23 | ///
provincie  == 24 | ///
provincie  == 25 | ///
provincie  == 29 | ///
provincie  == 30 | ///
provincie  == 31

keep formateur west_provincie
save "H:/Stata/Data/temp/mun_prov", replace

** Merging with admin data

use "$admin/admin_final", clear
gen age = year - yob
keep RINPERSOON RINPERSOONS residence age female year yob mob all_urban_40 all_urban_3040 region region3 pro2017 birthprovince vrlgbageboortegemeente
drop if residence == . 

gen muncode = residence 
gen str4 munstr4 = string(muncode, "%04.0f")
replace munstr4 = "GM" + munstr4
rename munstr4 formateur

merge m:1 formateur year using "$mun/munshort_v2"
keep if _merge == 3 
drop _merge

save "$temp/admin_mun_v2", replace
